import org.omg.CORBA.TIMEOUT;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.*;

public class coffeeGui extends JFrame{

    int coffeeNum = 0;
    int teaNum = 100;
    int waterNum = 4000;
    int sugarNum = 100;
    int cupNum = 100;
    int temperature = 100;   //温度
    int money = 0;         //币值

    int temF = 0;//判断加热情况

    int warnF = 1;   //判断资源是否充足

    int startF = 0; //判断是否取出

    int finish = 0;  //判断饮品是否完成制作


   public coffeeGui(){
       setTitle("咖啡机");    //设置显示窗口标题
       setBounds(200,200,522,1289);    //设置窗口显示位置及尺寸
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //关闭方式

       //字体
       Font font = new Font("仿宋", Font.BOLD, 17);

       //颜色设置
       Color color1 = new Color(199,232,255);
       Color color2 = new Color(207,206,205);
       Color color3 = new Color(128,202,204);
       setBackground(color2);

       //图片调用
       URL url =coffeeGui.class.getResource("企鹅.jpg");
       JLabel picture1 = new JLabel();
       Icon icon = new ImageIcon((url));
       picture1.setIcon(icon);
       picture1.setOpaque(true);

       URL url1 =coffeeGui.class.getResource("java.jpg");
       JLabel picture2 = new JLabel();
       Icon icon1 = new ImageIcon((url1));
       picture2.setIcon(icon1);
       picture2.setOpaque(true);

       //警告颜色标志
       JPanel waterWarn = new JPanel();
       waterWarn.setPreferredSize(new Dimension(20,20));
       waterWarn.setBackground(Color.green);

       JPanel coffeeWarn = new JPanel();
       coffeeWarn.setPreferredSize(new Dimension(20,20));
       coffeeWarn.setBackground(Color.green);

       JPanel milkWarn = new JPanel();
       milkWarn.setPreferredSize(new Dimension(20,20));
       milkWarn.setBackground(Color.green);

       JPanel sugarWarn = new JPanel();
       sugarWarn.setPreferredSize(new Dimension(20,20));
       sugarWarn.setBackground(Color.green);

       JPanel cupWarn = new JPanel();
       cupWarn.setPreferredSize(new Dimension(20,20));
       cupWarn.setBackground(Color.green);

       //add警告标签
       JLabel waterLabel = new JLabel("请加水");
       waterLabel.setPreferredSize(new Dimension(75,20));
       waterLabel.setFont(font);

       JLabel coffeeLabel = new JLabel("请加咖啡");
       coffeeLabel.setPreferredSize(new Dimension(75,20));
       coffeeLabel.setFont(font);

       JLabel milkLabel = new JLabel("请加奶茶");
       milkLabel.setPreferredSize(new Dimension(75,20));
       milkLabel.setFont(font);

       JLabel sugarLabel = new JLabel("请加糖");
       sugarLabel.setPreferredSize(new Dimension(75,20));
       sugarLabel.setFont(font);

       JLabel cupLabel = new JLabel("请加杯");
       cupLabel.setPreferredSize(new Dimension(75,20));
       cupLabel.setFont(font);

       //警告面板
       JPanel warnPanel = new JPanel();
       warnPanel.setPreferredSize(new Dimension(105,140));
       warnPanel.setBackground(color1);
       warnPanel.add(waterWarn);
       warnPanel.add(waterLabel);
       warnPanel.add(coffeeWarn);
       warnPanel.add(coffeeLabel);
       warnPanel.add(milkWarn);
       warnPanel.add(milkLabel);
       warnPanel.add(sugarWarn);
       warnPanel.add(sugarLabel);
       warnPanel.add(cupWarn);
       warnPanel.add(cupLabel);


       //显示标签
       JLabel temperatureLabel= new JLabel("水温:");
       temperatureLabel.setPreferredSize(new Dimension(50,20));
       temperatureLabel.setFont(font);

       JLabel temNum = new JLabel(temperature+"℃");
       temNum.setPreferredSize(new Dimension(60,20));
       temNum.setFont(font);

       JLabel moneyLabel = new JLabel("币值：");
       moneyLabel.setPreferredSize(new Dimension(70,20));
       moneyLabel.setFont(font);

       JLabel moneyNum = new JLabel(String.valueOf(money));
       moneyNum.setPreferredSize(new Dimension(30,20));
       moneyNum.setFont(font);

       //显示面板
       JPanel showPanel = new JPanel();
       showPanel.setPreferredSize(new Dimension(240,30));
       showPanel.setBackground(color1);
       showPanel.add(temperatureLabel);
       showPanel.add(temNum);
       showPanel.add( moneyLabel);
       showPanel.add(moneyNum );

       //按钮操作1
       JButton weakCoffee = new JButton("淡咖啡");
       weakCoffee.setPreferredSize(new Dimension(100,30));

       JButton strongCoffee = new JButton("浓咖啡");
       strongCoffee.setPreferredSize(new Dimension(100,30));

       JButton teaMilk = new JButton("奶茶");
       teaMilk.setPreferredSize(new Dimension(100,30));

       //按钮操作2
       JButton addWater = new JButton("加水");
       addWater.setPreferredSize(new Dimension(90,25));

       JButton addPowder = new JButton("加粉");
       addPowder.setPreferredSize(new Dimension(90,25));

       JButton addSugar = new JButton("加糖");
       addSugar.setPreferredSize(new Dimension(90,25));

       JButton addCup = new JButton("加杯");
       addCup.setPreferredSize(new Dimension(90,25));

       //按钮操作3
       JButton throwCoin = new JButton("投币");
       throwCoin.setPreferredSize(new Dimension(100,35));

       JButton backCoin = new JButton("退币");
       backCoin.setPreferredSize(new Dimension(100,35));

       JButton takeOut = new JButton("取出");
       takeOut.setPreferredSize(new Dimension(100,35));


       //淡咖啡
       weakCoffee.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(temF == 0){
                   if(startF == 0){
                       if(waterNum >= 200 && coffeeNum >= 5 && cupNum >= 1 && sugarNum >= 1){
                           if(money >= 1){
                               System.out.println("资源充足，开始制作淡咖啡！");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("淡咖啡制作完成，请先取走饮品!");
                               finish = 1;
                               startF = 1;
                               waterNum -= 200;
                               coffeeNum -= 5;
                               cupNum--;
                               sugarNum--;
                               money--;
                               moneyNum.setText(String.valueOf(money));
                           }else {
                               System.out.println("币值不够，请投币！！!");
                           }
                       }else {
                           warnF = 0;
                           System.out.println("资源存在耗尽情况，请先下方按照指示进行添加！");
                           if(waterNum < 200) {
                               System.out.println("水资源已经耗尽，请加水！！！");
                               waterWarn.setBackground(Color.red);
                           }
                           if (coffeeNum < 5) {
                               System.out.println("咖啡资源已经耗尽，请加咖啡！！！");
                               coffeeWarn.setBackground(Color.red);
                           }
                           if(sugarNum <= 0){
                               System.out.println("糖资源已经耗尽，请加糖！！！");
                               sugarWarn.setBackground(Color.red);
                           }
                           if (cupNum <= 0) {
                               System.out.println("杯子已经耗尽，请加杯子！！！");
                               cupWarn.setBackground(Color.red);
                           }
                       }
                   }else {
                       System.out.println("请先取走饮品，再来进行购买！");
                   }
               }else {
                   System.out.println("正在加热中，请等加热结束，再来进行购买！");
               }
           }
       });

       //浓咖啡
      strongCoffee.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(temF == 0){
                   if(startF == 0){
                       if(waterNum >= 200 && coffeeNum >= 10 && cupNum >= 1 && sugarNum >= 1){
                           if(money >= 1){
                               System.out.println("资源充足，开始制作淡咖啡！");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("浓咖啡制作完成，请先取走饮品!");
                               finish = 1;
                               startF = 1;
                               waterNum -= 200;
                               coffeeNum -= 10;
                               cupNum--;
                               sugarNum--;
                               money--;
                               moneyNum.setText(String.valueOf(money));
                           }else {
                               System.out.println("币值不够，请投币！！!");
                           }
                       }else {
                           warnF = 0;
                           System.out.println("资源存在耗尽情况，请先下方按照指示进行添加！");
                           if(waterNum < 200) {
                               System.out.println("水资源已经耗尽，请加水！！！");
                               waterWarn.setBackground(Color.red);
                           }
                           if (coffeeNum < 10) {
                               System.out.println("咖啡资源已经耗尽，请加咖啡！！！");
                               coffeeWarn.setBackground(Color.red);
                           }
                           if(sugarNum <= 0){
                               System.out.println("糖资源已经耗尽，请加糖！！！");
                               sugarWarn.setBackground(Color.red);
                           }
                           if (cupNum <= 0) {
                               System.out.println("杯子已经耗尽，请加杯子！！！");
                               cupWarn.setBackground(Color.red);
                           }
                       }
                   }else {
                       System.out.println("请先取走饮品，再来进行购买！");
                   }
               }else {
                   System.out.println("正在加热中，请等加热结束，再来进行购买！");
               }
           }
       });

      //奶茶
       teaMilk.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if( temF== 0){
                   if(startF == 0){
                       if(waterNum >= 200 && teaNum >= 10 && cupNum >= 1 && sugarNum >= 1){
                           if(money >= 1){
                               System.out.println("资源充足，开始制作淡咖啡！");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("...");
                               System.out.println("奶茶制作完成，请先取走饮品!");
                               finish = 1;
                               startF = 1;
                               waterNum -= 200;
                               teaNum -= 10;
                               cupNum--;
                               sugarNum--;
                               money--;
                               moneyNum.setText(String.valueOf(money));
                           }else {
                               System.out.println("币值不够，请投币！！!");
                           }
                       }else {
                           warnF = 0;
                           System.out.println("资源存在耗尽情况，请先下方按照指示进行添加！");
                           if(waterNum < 200) {
                               System.out.println("水资源已经耗尽，请加水！！！");
                               waterWarn.setBackground(Color.red);
                           }
                           if (teaNum < 10) {
                               System.out.println("奶茶资源已经耗尽，请加咖啡！！！");
                               milkWarn.setBackground(Color.red);
                           }
                           if(sugarNum <= 0){
                               System.out.println("糖资源已经耗尽，请加糖！！！");
                               sugarWarn.setBackground(Color.red);
                           }
                           if (cupNum <= 0) {
                               System.out.println("杯子已经耗尽，请加杯子！！！");
                               cupWarn.setBackground(Color.red);
                           }
                       }
                   }else {
                       System.out.println("请先取走饮品，再来进行购买！");
                   }
               }else {
                   System.out.println("请先取走饮品，再来进行购买！");
               }
           }
       });

       //投币
       throwCoin.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(startF == 0){
                   if(warnF == 1){
                       money++;
                       moneyNum.setText(String.valueOf(money));
                   }else {
                       System.out.println("资源存在耗尽情况，请先进行添加资源！");
                   }
               }else {
                   System.out.println("请先取走饮品，再来此操作！");
               }
           }
       });

       //退币
       backCoin.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(startF == 0){
                   money--;
                   moneyNum.setText(String.valueOf(money));
               }else {
                   System.out.println("请先取走饮品，再来此操作！");
               }
           }
       });

       //取出
       takeOut.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(finish == 1){
                   System.out.println("顾客取走饮品！期待下次光临！");
                   startF = 0;
                   finish = 0;
               }else {
                   System.out.println("请先完成饮品制作再来取走！");
               }
           }
       });

       //加水
       addWater.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(startF == 0){
                   waterNum = waterNum + 2000;
                   warnF = 1;
                   waterWarn.setBackground(Color.green);
                   System.out.println("加水2000cc,温度下降10℃");
                   temperature = temperature - 10;
                   System.out.println("当前温度为："+temperature+"  水容量为："+waterNum);
                   temNum.setText(temperature+"℃");
                   if(temperature < 70){
                       System.out.println("温度低于70摄氏度，开始加热！");
                       temF = 1;
                       Thread thread1 =new Thread(new Runnable() {
                           @Override
                           public void run() {
                               while (temperature < 100){
                                   try {
                                       Thread.sleep(1000);
                                   } catch (InterruptedException e) {
                                       e.printStackTrace();
                                   }
                                   temperature += 2;
                                   temNum.setText(temperature+"℃");
                               }
                               temF = 0;
                           }
                       });
                       thread1.start();
                   }
               }else {
                   System.out.println("请先取走饮品，再来此操作！");
               }
           }
       });

       //加粉
       addPowder.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
                if(startF == 0){
                    coffeeNum += 100;
                    teaNum += 100;
                    warnF = 1;
                    System.out.println("加咖啡100份，加奶茶100份");
                    System.out.println("当前咖啡容量为："+coffeeNum+"  当前奶茶容量为："+ teaNum);
                    milkWarn.setBackground(Color.green);
                    coffeeWarn.setBackground(Color.green);

                }else {
                    System.out.println("请先取走饮品，再来此操作！");
                }
           }
       });

       //加糖
       addSugar.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(startF == 0){
                    sugarNum += 10;
                    warnF = 1;
                    sugarWarn.setBackground(Color.green);
                    System.out.println("加糖10份");
                    System.out.println("当前糖容量为："+sugarNum);
               }else {
                   System.out.println("请先取走饮品，再来此操作！");
               }
           }
       });

       //加杯
       addCup.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               if(startF == 0){
                   cupNum += 10;
                   warnF = 1;
                   cupWarn.setBackground(Color.green);
                   System.out.println("加杯子10只");
                   System.out.println("当前杯子容量为："+cupNum);
               }else {
                   System.out.println("请先取走饮品，再来此操作！");
               }
           }
       });

       //主面板
       setVisible(true);    //设置窗口是否可见
       getContentPane().setLayout(null);//设置空布局，组件想怎么放怎么放

       //图片1插入
       JPanel picturePanel1 = new JPanel();
       picturePanel1.setBounds(0, 20, 500, 375);
       picturePanel1.add(picture1);
       getContentPane().add(picturePanel1);//放入jframe界面

       //第一个JPanel
       JPanel panel1 = new JPanel();
       panel1.setBounds(0, 395, 115, 250);
       panel1.setBackground(color2);
       panel1.add(warnPanel);
       panel1.add(weakCoffee);
       panel1.add(strongCoffee);
       panel1.add(teaMilk);
       getContentPane().add(panel1);//将panel放入jframe界面

       //第二个JPanel
       JPanel panel2 = new JPanel();
       panel2.setBounds(125, 395, 250, 250);
       panel2.setBackground(color3);
       panel2.add(showPanel);
       getContentPane().add(panel2);//将panel放入jframe界面

       //按钮JPanel
       JPanel panelButton = new JPanel();
       panelButton.setPreferredSize(new Dimension(105,130));
       panelButton.add(throwCoin);
       panelButton.add(backCoin);
       panelButton.add(takeOut);
       panelButton.setBackground(color1);

       //第三个JPanel
       JPanel panel3 = new JPanel();
       panel3.setBounds(385, 395, 115, 250);
       panel3.setBackground(color2);
       panel3.add(addWater);
       panel3.add(addPowder);
       panel3.add(addSugar);
       panel3.add(addCup);
       panel3.add(panelButton);
       getContentPane().add(panel3);//将panel放入jframe界面

       //图片2插入
       JPanel picturePanel2 = new JPanel();
       picturePanel2.setBounds(0, 645, 500, 594);
       picturePanel2.add(picture2);
       getContentPane().add(picturePanel2);//将panel放入jframe界面
   }

    public static void main(String[] args) {

        coffeeGui gui = new coffeeGui();
        gui.setVisible(true);


    }
}


